package alpha2;

import java.net.URI;
import java.nio.charset.Charset;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
import org.java_websocket.handshake.ServerHandshake;


public class Handle extends Common{

	public static void get() throws Exception{
		System.out.println("Start GET /handle 获取所有Handle ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/handle");

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	public static void add(Integer cameraId, String groupId, String attachedParams) throws Exception {
        System.out.println("Start POST /handle 创建 Handle ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost request = new HttpPost(HOST + "/handle");
        
        MultipartEntity reqEntity = new MultipartEntity();  
        reqEntity.addPart("camera_id", new StringBody(cameraId.toString()));
        reqEntity.addPart("group_id", new StringBody(groupId));
        if(null != attachedParams){
        	reqEntity.addPart("attached_params", new StringBody(attachedParams,Charset.forName("UTF-8")));
        }
        request.setEntity(reqEntity); 

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	
	public static void delete(Integer handleId) throws Exception{
		System.out.println("Start DELETE /handle/{handle_id} 删除单个 Handle ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpDelete request = new HttpDelete(HOST + "/handle/" + handleId);

        //发起网络请求
        httpclient.execute(request);
        
	}
	
	public static void get(Integer handleId) throws Exception{
		System.out.println("Start GET /handle/{handle_id} 获取单个 Handle ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/handle/" + handleId);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	
	public static void update(Integer handleId, Integer cameraId, String groupId, String attached_params) throws Exception {
        System.out.println("Start PUT /handle/{handle_id} 修改单个 Handle ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPut request = new HttpPut(HOST + "/handle/" + handleId);
        
        MultipartEntity reqEntity = new MultipartEntity();  
        reqEntity.addPart("handle_id", new StringBody(handleId.toString()));
        reqEntity.addPart("camera_id", new StringBody(cameraId.toString()));
        reqEntity.addPart("group_id", new StringBody(groupId));
        reqEntity.addPart("attached_params", new StringBody(attached_params));
        request.setEntity(reqEntity); 

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	
	public static void start(Integer handleId) throws Exception {
        System.out.println("Start POST /handle/{handle_id}/start 打开 Handle ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost request = new HttpPost(HOST + "/handle/" + handleId + "/start");

        //发起网络请求
        httpclient.execute(request);
        
    }
	
	public static void stop(Integer handleId) throws Exception {
        System.out.println("Start POST /handle/{handle_id}/stop 停止 Handle ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost request = new HttpPost(HOST + "/handle/" + handleId + "/stop");

        //发起网络请求
        httpclient.execute(request);
        
    }
	
	public static void live(Integer handleId){
		System.out.println("GET /handle/{handle_id}/live 获取 Handle 的输出 ...");
		URI uri = URI.create(WS_HOST + "/handle/" + handleId + "/live");
		WebSocketClient webSocketClient = new WebSocketClient(uri, new Draft_17()) {
			@Override
			public void onOpen(ServerHandshake serverHandshake) {
				System.out.println("webSocketClient open " + serverHandshake.getHttpStatusMessage());
			}

			@Override
			public void onMessage(String s) {
				System.out.println("webSocketClient onMessage " + s);
			}

			@Override
			public void onClose(int i, String s, boolean b) {
				System.out.println("webSocketClient onClose " + s + " " + i + " " + b);
			}

			@Override
			public void onError(Exception e) {
				System.out.println("webSocketClient onError " + e.getMessage());
			}
		};
		webSocketClient.connect();
	}
	
	
}
